package pms.dao;


import java.util.List;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import pms.om.Theater;

/**
 * The Data access class for theaters. All Interaction with the database
 * regarding the entity bean Theater should be handled by this class!
 * @author KRS DEV Team (krsdevteam@PMSWIbb2011SS.de)
 */
public class TheaterDao extends HibernateDaoSupport {
	private TheaterDao theaterDao;
	
	public TheaterDao getTheaterDao() {
		return theaterDao;
	}

	public void setTheaterDao(TheaterDao theaterDao) {
		this.theaterDao = theaterDao;
	}

	/**
	 * returns a single theater by its primary db key
	 * @param id
	 * @return a single Boot
	 */
	public Theater findById(int id) {
		HibernateTemplate template = getHibernateTemplate();
		return (Theater) template.get(Theater.class, id);
	}
	
	/**
	 * Returns all theaters from the database.
	 * @return a list of Theaters
	 * @see Boot
	 */
	@SuppressWarnings("unchecked")
	public List<Theater> findAll() {
		HibernateTemplate template = getHibernateTemplate();
			
		return template.loadAll(Theater.class);
	}

	
	
	/**
	 * modifies an existing theater in the database which is selected by its primary key.
	 * @param id the primary key of the theater.
	 * @param name the new name for the theater
	 * @param plaetze_loge the new number of of seats in the loge 
	 * @param plaetze_reg the new number of of seats regular
	 * @see Theater
	 */
	public void modify(int id, String name, int plaetze_loge, int plaetze_reg) {
		HibernateTemplate template = getHibernateTemplate();
		Theater theater = (Theater) template.get(Theater.class, id);
		theater.setName(name);
		theater.setPlaetze_loge(plaetze_loge);
		theater.setPlaetze_reg(plaetze_reg);		
		
		template.saveOrUpdate(theater);
	}

	/**
	 * creates a new persistent theater in the database providing all the necessary details.
	 * @param name the name for the new theater
	 * @param plaetze_loge the number of seats in the loge
	 * @param plaetze_reg the number of seats regular
	 * @see Boot
	 */
	public void create(String name, int plaetze_loge, int plaetze_reg) {
		HibernateTemplate template = getHibernateTemplate();
		Theater theater = new Theater(name, plaetze_loge, plaetze_reg);
		template.persist(theater);
	}


}